package com.qf.controller;


import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGroupon;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.GoodsService;
import com.qf.service.GroupService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RequestMapping("/admin/groupon")
@RestController
public class AdminGroupController {

    @Autowired
    GroupService groupService;
    @Autowired
    GoodsService goodsService;

    @RequestMapping("/list")
    public Object getList( @RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                           @RequestParam(value = "limit",required = false, defaultValue = "20")Integer limit ,
                           @RequestParam(value = "sort",required = false, defaultValue = "add_time") String sort,
                           @RequestParam(value = "order",required = false, defaultValue = "desc")String order,Integer goodsId) throws Exception {

        Subject subject = SecurityUtils.getSubject();

        DtsAdmin dtsAdmin = (DtsAdmin)subject.getPrincipal();

        Integer id = goodsId;

       /* if(subject.hasRole("推广管理员")){

        }*/
       /* if(!b){
            throw  new Exception("您不能查询");
        }*/
        Map<String,Object> data = groupService.selectList(page,limit,sort,order,id);
        return ResponseUtil.ok(data);

        //throw new Exception("您没有该权限");
    }
    @RequestMapping("/create")
    public Object create(@RequestBody DtsGrouponRules grouponRules){

        System.out.println(grouponRules);
        int goodsId = grouponRules.getGoodsId();
        DtsGoods goods = goodsService.selectById(goodsId);



        grouponRules.setAddTime(new Date());
        grouponRules.setGoodsName(goods.getName());
        groupService.create(grouponRules);
        return ResponseUtil.ok(grouponRules);

    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsGrouponRules dtsGrouponRules){


        groupService.update(dtsGrouponRules);
        return ResponseUtil.ok();
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsGrouponRules dtsGrouponRules){

        groupService.delete(dtsGrouponRules);
        return ResponseUtil.ok();
    }

}
